home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 August: Tool Chest / Dev.CD Aug 98 TC.toast / Sample Code / QuickTime / Show Movie / Sources / WindStuff.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-20  |  6.7 KB  |  253 lines  |  [TEXT/MMCC]

  1. /*
  2.   File:            WindStuff.c
  3.   Contains:        Window handling routines.
  4.   Written by:    Jason Hodges-Harris & Don Swatman
  5.   Copyright:    © 1995 by Apple Computer, Inc., all rights reserved.
  6. */
  7.  
  8.  
  9. #include <Dialogs.h>
  10.  
  11. #include "WindStuff.h"
  12.  
  13. #include "MenuStuff.h"
  14. #include "MovieStuff.h"
  15.  
  16. //==============================================
  17. //  Globals                                     
  18. //==============================================
  19. WindowPtr gTheWinds[kMaxWindows];  // holds the list of windows
  20. Boolean      gDone;                   // Set to true to make the program quit
  21.  
  22. //==============================================
  23. //  AboutBox                                   
  24. //
  25. // This draws the about box. It also shows how to
  26. // use the tool box to draw a default box around
  27. // the OK box
  28. //==============================================
  29.  
  30. void AboutBox(void)
  31. {
  32.   GrafPtr   savePort = nil;
  33.     DialogPtr aboutDialog;
  34.     ModalFilterUPP theFilter = nil;
  35.     short     itemHit = 0;   // dialog item we've clicked on
  36.     
  37. // Get the dialog box resource
  38.     aboutDialog = GetNewDialog(9041, nil, (WindowPtr) -1 );
  39.  
  40.   GetPort(&savePort);
  41.   SetPort( aboutDialog );
  42.  
  43.     ShowWindow( aboutDialog );
  44.         
  45. // Get the standard filter proc
  46.   if (GetStdFilterProc(&theFilter) != noErr)
  47.       DebugStr("\pFailed to get standard dialog filter.");
  48.   
  49. // Set item 1 - <OK> to have a default box around it
  50.     SetDialogDefaultItem(aboutDialog,1);
  51.   
  52. // Modal dialog loop    
  53. do     {
  54. // Use "theFilter" in ModalDialog call
  55.        ModalDialog(theFilter,&itemHit);
  56.     } while (itemHit != 1);
  57.  
  58.   DisposeDialog(aboutDialog);
  59.   SetPort(savePort);
  60. }
  61.  
  62. //==============================================
  63. //
  64. //  Gemeral Window handling Stuff
  65. //
  66. //==============================================
  67.  
  68. //----------------------------------------------
  69. //  GetWindowNum
  70. //
  71. // Gets the number of a window in the list from
  72. // it's WindowPtr
  73. //----------------------------------------------
  74. short GetWindowNum ( WindowPtr pWindow )
  75. {
  76.     short windCount;
  77.     short foundWind = -1;
  78.  
  79.     if (pWindow)
  80.         for (windCount=0; (windCount < kMaxWindows) && (foundWind == -1); windCount++)
  81.             if (gTheWinds[windCount] == pWindow)
  82.                 foundWind = windCount;
  83.     
  84.     return ( foundWind);
  85. }
  86.  
  87. //----------------------------------------------
  88. //  IsOurWindow
  89. //
  90. // Find out if this is one of our window
  91. //----------------------------------------------
  92. Boolean IsOurWindow ( WindowPtr pWindow )
  93. {
  94.     return ( GetWindowNum(pWindow) != -1 );
  95. }
  96.  
  97. //----------------------------------------------
  98. // IsFreeWind
  99. //
  100. // Find out if we can add "howManyNeeded" more windows
  101. //   and return a number to the first free
  102. //----------------------------------------------
  103.  
  104. Boolean IsFreeWind( short *newWindNum, short howManyNeeded )
  105. {
  106.     short windCount;   // Current window list entry we're looking at
  107.     short leftToFind;  // How many more windows we need to find
  108.     
  109.     leftToFind  = howManyNeeded;
  110.     *newWindNum = -1;
  111.     for (windCount=0; (windCount < kMaxWindows) && (leftToFind != 0 ); windCount++)
  112.         if (gTheWinds[windCount] == nil)
  113.             {
  114.                 --leftToFind;
  115.                 if (*newWindNum == -1 )
  116.                     *newWindNum = windCount;
  117.             }
  118.     return(leftToFind == 0);
  119. }
  120.  
  121. //----------------------------------------------
  122. //  CloseOurWindow
  123. //
  124. // Close a window pointed to by "pWindow"
  125. // Also if the window is a master movie, then 
  126. // recursively close it's slave window
  127. //----------------------------------------------
  128. void CloseOurWindow ( short windNum )
  129. {
  130.     short     slaveWindNum = -1;   // slave window num
  131.     WindowPtr pSlaveWindow = nil;  // slave WindowPtr
  132.     
  133.     if (gTheWinds[windNum])                   // Check it's not nil
  134.         if (IsOurWindow(gTheWinds[windNum]))    // Check it's ours
  135.         {
  136.             CloseMovieWindow( gTheWinds[windNum],
  137.                                                 &pSlaveWindow );    // kills all the movie stuff
  138.             DisposeWindow(gTheWinds[windNum]);    // dispose of the window
  139.             gTheWinds[windNum] = nil;             // Return nil in pWindow            
  140.             DoAdjustMenus();                        // update the menus
  141.  
  142. // Recursively remove the window's slave
  143.             if (pSlaveWindow)
  144.             {
  145.                 slaveWindNum = GetWindowNum ( pSlaveWindow );
  146.                 if (slaveWindNum != -1 )
  147.                     CloseOurWindow ( slaveWindNum );
  148.             }
  149.         }
  150. }
  151.  
  152. //----------------------------------------------
  153. //   CloseAllWindows
  154. //
  155. //  Scans down the window list and closes each
  156. //  of the windows
  157. //----------------------------------------------
  158.  
  159. void CloseAllWindows(void)
  160. {
  161.     short windCount;
  162.  
  163.     for (windCount = 0; windCount < kMaxWindows; windCount++)
  164.         if (gTheWinds[windCount] != nil)
  165.             CloseOurWindow( windCount );
  166. }
  167.  
  168. //----------------------------------------------
  169. // DoWindUpdate
  170. //
  171. // Updates a window
  172. //----------------------------------------------
  173.  
  174. void    DoWindUpdate ( WindowPtr pWindow )
  175. {    
  176.     if (IsOurWindow(pWindow))
  177.     {
  178.         BeginUpdate (pWindow);
  179.  
  180. // update the movie window
  181.         UpdateMovieWindow ( pWindow );
  182.         
  183.         EndUpdate (pWindow);
  184.     }
  185. }
  186.  
  187. //----------------------------------------------
  188. //  DragSelWind
  189. //
  190. // Handles a click in the drag area of a window
  191. // and drags selected window around desktop
  192. //----------------------------------------------
  193. void DragSelWind( WindowPtr window,
  194.                                     Point     mouseLoc)
  195. {
  196.     Rect    dragBounds;
  197.     
  198.     dragBounds = (**GetGrayRgn()).rgnBBox;   // Get the rect the window can be dragged around
  199.     DragWindow(window,mouseLoc,&dragBounds); // Now Drag it
  200. }
  201.  
  202. //----------------------------------------------
  203. //  DoGoAwayWind
  204. //
  205. // Handles mouse down event in the go away box
  206. // and closes the window if neccessary
  207. //----------------------------------------------
  208. void DoGoAwayWind ( WindowPtr pWindow,
  209.                                         Point     mouseLoc)
  210. {
  211.     short windNum;
  212.     if (TrackGoAway(pWindow,mouseLoc))
  213.     {
  214.         if (pWindow)
  215.         {
  216.             windNum = GetWindowNum ( pWindow );
  217.             if (windNum != -1 )
  218.                 CloseOurWindow( windNum );
  219.         }
  220.     }
  221. }
  222.  
  223. //----------------------------------------------
  224. //   CreateWindow
  225. //
  226. // Create an empty window,
  227. //   but doesn't show it just yet
  228. //----------------------------------------------
  229.  
  230. void CreateWindow ( short windNum, Str255 theTitle, WindowPtr pWindowBehind )
  231. {
  232.     Rect  windRect;      // Initial window size
  233.     short diagOffset;    // offset from top left of window
  234.     
  235. // Calculate where we want to put the window and give it an initial size of 50,50
  236.     diagOffset = 50 * windNum;
  237.     SetRect( &windRect, 50,50,100,100 );
  238.     OffsetRect ( &windRect, diagOffset, diagOffset );
  239.  
  240.     gTheWinds[windNum] = (WindowPtr) NewCWindow ( nil,            // Create Storage
  241.                                                                                                  &windRect,      // Rect to put the window in
  242.                                                                                                  theTitle,       // Windows Title
  243.                                                                                                  false,          // Not Visible
  244.                                                                                                  noGrowDocProc,  // Ordinary window without grow box
  245.                                                                                                 pWindowBehind,  // Window this one behind
  246.                                                                                                  true,           // Has Go away box
  247.                                                                                                  0 );            // no Refcon (we'll use this later)
  248.                                                                                                                  //    to store movie info
  249.  
  250. }
  251.  
  252.  
  253.